package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.common.R;
import com.example.reggie.dto.DishDto;
import com.example.reggie.entity.Dish;
import com.example.reggie.entity.DishFlavor;
import com.example.reggie.exception.CustomException;
import com.example.reggie.mapper.DishMapper;
import com.example.reggie.service.DishFlavorService;
import com.example.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //菜品id回填
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 修改一个菜品
     * @param
     */
    @Transactional
    public boolean updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);

        Long dishId=dishDto.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        boolean remove = dishFlavorService.remove(queryWrapper);
        if (!remove){
            return false;
        }
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
        return true;
    }


    /**
     * 起停售菜品
     * @param
     *  "拦截到请求：/dish/status/status
     *  [1600771040805441538,1397849739276890114]
     */
    @Transactional
    public boolean updateWithStatus(String id, int status) {
        String[] list=id.split(",");
        for (int i=0;i<list.length;i++){
            Long did=Long.parseLong(list[i]);
            Dish dish = new Dish();
            dish.setId(did);
            dish.setStatus(status);
            boolean b = this.updateById(dish);
            if (!b){
                return false;
            }
        }
        return true;
    }

    /**
     * 删除菜品
     * @param
     *  "拦截到请求：/dish/
     */
    @Transactional
    public boolean delMulity(String ids) {
        String[] list=ids.split(",");
        Long id=0L;
        for (int i=0;i< list.length;i++){
            id=Long.parseLong(list[i]);
            boolean b=this.removeById(id);
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(id!=null,DishFlavor::getDishId,id);
            boolean a = dishFlavorService.remove(queryWrapper);
        }
        return true;
    }




}
